<?php
/*
 * kpi绩效考核管理
 * 
 * @author  Lee <a605333742@gmail.com>
 * @time    2016-07-21
 */
namespace Home\Controller;

class KpiController extends AllowController{
    /*
     * 构造函数
     *
     * @return  #
     */
    public function _initialize(){       
        parent::_initialize();
        $this->db_kpi       =D('Kpi');
        $this->db_kpiTasks  =D('KpiTasks');
    } 
    
    /*
     * 当前正在进行的考核及部分列表
     * 
     * @return #
     */
    public function index(){
        $where=array(
            "status"    =>1,
            "accept_id" =>session("home.id"),
        );
        $ord['add_time']=desc;
        $kpiInfo        =$this->db_kpi->where($where)->order($ord)->find();
        $kpiTasks       =$this->db_kpiTasks->where(array("kpi_id"=>$kpiInfo['id'],'status'=>1))->select();
        //统计任务数量
        $taskNum        =$this->db_kpiTasks->where(array("kpi_id"=>$kpiInfo['id'],'status'=>1))->count();        
        $where1=array(
            "status"    =>1,
            "accept_id" =>session('home.id'),
            "add_time"  =>array("lt",$kpiInfo['add_time'])
        );
        $kpiPage        =$this->db_kpi->kpiList($where1,$ord,10,1);
        $kpiList        =$kpiPage['kpiList'];
        $pageList       =$kpiPage['pageList'];
        if($kpiInfo['is_gongbu']){
            $this->assign("kpiInfo",$kpiInfo);
        }
        $this->assign("pageList",$pageList);
        $this->assign("kpiList",$kpiList);
        $this->assign("kpiTasks",$kpiTasks);
        $this->assign("taskNum",$taskNum);
        $this->assign("kpi_id",$kpiInfo['id']);
        
        $this->display();
    }
    
    public function jsonTasks(){
        if($this->db_kpiTasks->where(array("kpi_id"=>I("request.kpi_id"),'status'=>1))->getField("self_assessment")){
            echo 3;
            exit;
        }
        $idarr  =I("request.idarr");      
        $arr    =I("request.arr");
        $arrval =array();    
        foreach($arr as $k=>$v){
            $arrval[]=$v;
        }
        foreach(array_combine($idarr,$arrval) as $k=>$v){
            $where['id']                =$k;
            $where['self_assessment']   =$v;
            $result[]=$this->db_kpiTasks->save($where);
        }
        if($result){
            $map['id']      =I("request.kpi_id");
            $map['is_self'] =1;
            $this->kpi->save($map);
            echo 1;
        }else{
            echo 2;
        }
    }
}



